package com.ruoyi.lxzx.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.lxzx.appDTO.lxjhDTO.UserActivitySaveDTO;
import com.ruoyi.lxzx.appDTO.lxlbDTO.SearchDTO;
import com.ruoyi.lxzx.appDTO.lxlbDTO.SqrqshDTO;
import com.ruoyi.lxzx.appDTO.lxlbDTO.UserCardDto;
import com.ruoyi.lxzx.appVO.grzxVO.WdjhVO;
import com.ruoyi.lxzx.appVO.lxjhVO.BmrVO;
import com.ruoyi.lxzx.appVO.lxjhVO.hdjhDetailVO;
import com.ruoyi.lxzx.appVO.lxjhVO.hdjhVO;
import com.ruoyi.lxzx.appVO.lxlbVO.LxlbListDetailVO;
import com.ruoyi.lxzx.appVO.lxlbVO.LxlbListVO;
import com.ruoyi.lxzx.appVO.lxqlVO.GroupDetail;
import com.ruoyi.lxzx.appVO.lxqlVO.GroupMemberListVO;
import com.ruoyi.lxzx.appVO.lxqlVO.GroupMessageVO;
import com.ruoyi.lxzx.appVO.lxqlVO.GroupVO;
import com.ruoyi.lxzx.appVO.lxxmVO.UserCircleDetailVO;
import com.ruoyi.lxzx.appVO.lxxmVO.UserCircleDiscussVO;
import com.ruoyi.lxzx.domain.*;
import com.ruoyi.lxzx.service.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class AppServiceImpl implements IAppService {

    private final IUserCardService userCardService;
    private final IUserAttentService userAttentService;
    private final IUserActivityService userActivityService;
    private final IActivityApplyService activityApplyService;
    private final IGroupService groupService;
    private final IGroupMemberService groupMemberService;
    private final IGroupMessageService groupMessageService;
    private final IUserCircleService userCircleService;
    private final IUserCircleLikeService circleLikeService;
    private final IUserCircleDiscussService circleDiscussService;
    private final IUserIntegralService userIntegralService;
    private final IUserIntegralDetailService userIntegralDetailService;
    private final IUserRealService userRealService;
    private WxMaService wxMaService;


    @Override
    public List<LxlbListVO> lxlb(SearchDTO param) {
        return userCardService.lxlb(param);
    }

    @Override
    public void gzlx(Long userId) {
        userAttentService.gzlx(userId);
    }

    @Override
    public LxlbListDetailVO ckmp(Long userId) {
        return userCardService.ckmp(userId);
    }
    @Override
    public int updateUserRemoveCard(UserCard userCard) {

        return userCardService.updateUserRemoveCard(userCard);
    }

    @Override
    public List<hdjhVO> hdjh() {
        return userActivityService.hdjh();
    }

    @Override
    public hdjhDetailVO hdjhxq(Long id) {
        return userActivityService.hdjhxq(id);
    }

    @Override
    public void hdbm(Long activityId, Long userId) {
        activityApplyService.hdbm(activityId, userId);
    }

    @Override
    public void fbhd(UserActivitySaveDTO userActivity) {
        userActivityService.fbhd(userActivity);
    }

    @Override
    public List<GroupVO> qlb() {
        return groupService.qlb();
    }

    @Override
    public List<GroupMemberListVO> qcy(Long groupId) {
        return groupMemberService.qcy(groupId);
    }

    @Override
    public List<GroupMessageVO> qlt(Long groupId) {
        return groupMessageService.qlt(groupId);
    }

    @Override
    public void sqrq(Long groupId) {
        groupMemberService.sqrq(groupId);
    }

    @Override
    public List<GroupVO> wdq() {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        return groupService.wdq(finalUserId);
    }

    @Override
    public void cjq(Group group) {
        groupService.insertGroup(group);
        GroupMember gm = new GroupMember();
        gm.setGroupId(group.getId());
        gm.setUserId(group.getUserId());
        gm.setStatus("2");
        gm.setCreateBy(group.getUserId() + "");
        gm.setCreateTime(new Date());
        groupMemberService.insertGroupMember(gm);
    }
    @Override
    public void xgq(Group group) {
        groupService.updateGroup(group);

    }

    @Override
    public void fbxm(UserCircle userCircle) {
        userCircleService.fbxm(userCircle);
    }

    @Override
    public PageInfo xmlb(SearchDTO param) {
        return userCircleService.xmlb(param);
    }

    @Override
    public UserCircleDetailVO xmxq(Long circleId) {
        return userCircleService.xmxq(circleId);
    }

    @Override
    public int xmdz(Long circleId) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        return circleLikeService.xmdz(circleId, finalUserId);
    }

    @Override
    public List<UserCircleDiscussVO> xmpl(Long circleId) {
        return circleDiscussService.xmpl(circleId);
    }

    @Override
    public String hqdz(Long userId) {
        return userCardService.hqdz(userId);
    }

    @Override
    public UserIntegral hqjf(Long userId) {
        return userIntegralService.hqjf(userId);
    }

    @Override
    public List<UserIntegralDetail> jfxq(Long userId) {
        return userIntegralDetailService.jfxq(userId);
    }

    @Override
    public WdjhVO wdjh() {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        return userActivityService.wdjh(finalUserId);
    }

    @Override
    public List<LxlbListVO> sqgl() {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        return userCardService.sqgl(finalUserId);
    }

    @Override
    public List<LxlbListVO> wdgz() {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        return userCardService.wdgz(finalUserId);
    }

    @Override
    public boolean xgmp(UserCardDto userCard) {
        return userCardService.updateUserCardApp(userCard);
    }

    @Override
    public List<BmrVO> hdjhybmr(Long activityId) {
        return userCardService.hdjhybmr(activityId);
    }

    @Override
    public LxlbListDetailVO dlckmp() {
        return userCardService.dlckmp();
    }

    @Override
    public void smrz(UserReal ur) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        UserReal userReal = this.userRealService.selectUserRealByUserId(finalUserId);
        if (userReal == null) {
            ur.setStatus("1");
            ur.setUserId(finalUserId);
            ur.setCreateTime(new Date());
            ur.setCreateBy(finalUserId + "");
            ur.setCreateTime(new Date());
            userRealService.insertUserReal(ur);
        }
    }

    @Override
    public void sqrqsh(SqrqshDTO shDto) {
        groupMemberService.sqrqsh(shDto);
    }

    @Override
    public GroupDetail qxq(Long groupId) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
        }
        Group g = groupService.selectGroupById(groupId);
        if (g == null) {
            throw new ServiceException("获取不到对应的信息");
        }
        GroupDetail gd = new GroupDetail();
        BeanUtils.copyProperties(g, gd);
        if (0L == finalUserId) {
            gd.setOwnStatus("0");
        } else {
            GroupMember gm = groupMemberService.findOwnStatus(gd.getId(), finalUserId);
            if (gm != null) {
                gd.setOwnStatus(gm.getStatus());
            } else {
                gd.setOwnStatus("0");
            }
        }
        return gd;
    }

    @Override
    public GroupMessage fsxx(GroupMessage gm) {
        try {
            boolean flag = wxMaService.getSecCheckService().checkMessage(gm.getContent());
            if (!flag) {
                throw new ServiceException("发布的内容不符合相关规定要求,请修改内容后在进行发布");
            }
        } catch (Exception e) {
            throw new ServiceException("发布的内容不符合相关规定要求,请修改内容后在进行发布");
        }
        gm.setStatus("1");
        gm.setTime(new Date());
        groupMessageService.insertGroupMessage(gm);
        return gm;
    }

    @Override
    public GroupMessage cxfsxx(GroupMessage gm) {
        gm.setStatus("2");
        groupMessageService.updateGroupMessage(gm);
        return gm;
    }

    @Override
    public GroupMessage scfsxx(GroupMessage gm) {
        gm.setStatus("3");
        groupMessageService.updateGroupMessage(gm);
        return gm;
    }

    @Override
    public void qxxmdz(Long circleId) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        circleLikeService.deleteUserCircleLikeById(circleId, finalUserId);
    }

    @Override
    public void scpl(Long id) {
        circleDiscussService.deleteUserCircleDiscussById(id);
    }

    @Override
    public void qxgzlx(Long userId) {
        userAttentService.qxgzlx(userId);
    }

    @Override
    public void fbpl(UserCircleDiscuss ucd) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        try {
            boolean flag = wxMaService.getSecCheckService().checkMessage(ucd.getContent());
            if (!flag) {
                throw new ServiceException("发布的内容不符合相关规定要求,请修改内容后在进行发布");
            }
        } catch (Exception e) {
            throw new ServiceException("发布的内容不符合相关规定要求,请修改内容后在进行发布");
        }
        ucd.setDiscussUserId(finalUserId);
        ucd.setCreateTime(new Date());
        ucd.setCreateBy(finalUserId + "");
        circleDiscussService.insertUserCircleDiscuss(ucd);
    }

    @Override
    public void gxxjd(String addressPresent) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        userCardService.gxxjd(addressPresent, finalUserId);
    }

    @Override
    public void hdqxbm(Long activityId, Long userId) {
        activityApplyService.hdqxbm(activityId, userId);
    }

    @Override
    public void xgtx(String img) {
        userCardService.xgtx(img);
    }

    @Override
    public List<GroupMessageVO> sxqlt(Long groupId, Long id) {
        return groupMessageService.sxqlt(groupId, id);
    }

    @Override
    public PageInfo wdxmlb(SearchDTO param) {
        return userCircleService.wdxmlb(param);
    }

    @Override
    public void scxm(Long circleId) {
        userCircleService.scxm(circleId);
    }

    @Override
    public void scq(Long groupId) {
        groupService.scq(groupId);
    }

    @Override
    public void scjh(Long activityId) {
        userActivityService.scjh(activityId);
    }
    @Override
    public void jhfill(Long activityId) {
        userActivityService.jhfill(activityId);
    }

}
